//
//  HeadTableViewCell.m
//  HYHOME
//
//  Created by Mac on 16/1/20.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "HeadTableViewCell.h"
#import "ModelData.h"
#import "UIImageView+WebCache.h"
#import "RESideMenu.h"
#import "HeadImageView.h"


@interface HeadTableViewCell ()<UIScrollViewDelegate>
@property (nonatomic,retain) UIScrollView *scrollView;
@property (nonatomic,retain) UIPageControl *pageC;
@property (nonatomic,retain) NSMutableArray *arrImages;

@property (nonatomic, retain) UIActivityIndicatorView *activity;


@property (nonatomic, retain) NSTimer *timer;


@end

@implementation HeadTableViewCell
-(void)dealloc{
    [_activity release];
    [_arrImages release];
    [_pageC release];
    [_scrollView release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {

    
        [self createScrollView];
        self.arrImages = [NSMutableArray array];
    }


    return self;

}


-(void)createScrollView{

    self.scrollView = [[UIScrollView alloc] init];

    self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    
    
    [self.scrollView addSubview:self.activity];

    [self.contentView addSubview:self.scrollView];
    
    
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    
    self.scrollView.bounces = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;

    
    self.pageC = [[UIPageControl alloc]init];

    [self.contentView addSubview:self.pageC];
    
    [self.scrollView release];
    [self.pageC release];
    [self.activity release];

    
    
}




- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.scrollView.frame =CGRectMake(0, 0, self.frame.size.width, 200);
    self.pageC.frame = CGRectMake(0, 0, self.frame.size.width / 2, 40);
    
    // 滚动点的中心点
    self.pageC.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height-20);
    
//    self.pageC.currentPage = 0;
    // page个数
    self.pageC.numberOfPages = self.arrImages.count - 2;
    
    self.pageC.backgroundColor = [UIColor blackColor];
    
    self.pageC.alpha = 0.3;
    
    for (int i = 0; i< self.arrImages.count; i++) {
        
        HeadImageView *imageV = [[HeadImageView alloc] init];
        
        ModelData *model = self.arrImages[i];
        
        [imageV sd_setImageWithURL:[NSURL URLWithString:model.imgurl] placeholderImage:[UIImage imageNamed:@"jz"]];
        
        imageV.frame = CGRectMake(self.frame.size.width *i, 0, self.frame.size.width, self.frame.size.height);
        
        
        self.scrollView.contentSize = CGSizeMake(self.frame.size.width *self.arrImages.count, self.frame.size.height);
        
        [self.scrollView addSubview:imageV];
        [imageV release];

        imageV.userInteractionEnabled = YES;
        imageV.tag = i + 1;
        imageV.delegate = self.delegate;
        
        
    }
    
    
    self.activity.center = self.scrollView.center;
    [self.activity startAnimating];
    
    
    [self addPage];
    
        
}

//轮播图自动播放

- (void)addPage
{
    
    
    if (!self.timer) {

        self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(autoplay) userInfo:nil repeats:YES];
        
        
    }

    
}



- (void)autoplay{

    CGFloat x = self.scrollView.contentOffset.x ;
    CGFloat width = self.scrollView.frame.size.width;
    
    if (x + width >=self.scrollView.contentSize.width) {
    
        
        [self.scrollView setContentOffset:CGPointMake(width, 0) animated:NO];
    
    }else{
    
        [self.scrollView setContentOffset:CGPointMake(x + width, 0) animated:YES];
    
    
    }
    
   // NSInteger a = self.scrollView.contentOffset.x/self.frame.size.width;
    
    if (self.scrollView.contentOffset.x == self.frame.size.width) {
        self.pageC.currentPage = 0;
    } if (self.scrollView.contentOffset.x == self.frame.size.width * 2) {
        self.pageC.currentPage = 1;
    } if (self.scrollView.contentOffset.x == self.frame.size.width * 3) {
        self.pageC.currentPage = 2;
    }
    
    
    
}



-(void)passModel:(NSMutableArray *)array {

    self.arrImages = array;
    
//    [self.arrImages addObject:self.arrImages.firstObject];
//    
//    [self.arrImages insertObject:[self.arrImages objectAtIndex:self.arrImages.count - 2] atIndex:0];
//    
    
    [self.activity stopAnimating];
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.timer invalidate];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(autoplay) userInfo:nil repeats:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.x == 0) {
        [self.scrollView setContentOffset:CGPointMake(self.frame.size.width *(self.arrImages.count - 2), 0)];
    }
    
    if (scrollView.contentOffset.x == self.frame.size.width * (self.arrImages.count - 1)) {
        
        [self.scrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
    }
    
    if (self.scrollView.contentOffset.x == self.frame.size.width) {
        self.pageC.currentPage = 0;
    } if (self.scrollView.contentOffset.x == self.frame.size.width * 2) {
        self.pageC.currentPage = 1;
    } if (self.scrollView.contentOffset.x == self.frame.size.width * 3) {
        self.pageC.currentPage = 2;
    } if (self.scrollView.contentOffset.x == self.frame.size.width * 4) {
        self.pageC.currentPage = 3;
    }
    
 

    
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
